package com.game.server.message.proto.execute;

import com.comm.ISession;

import com.game.majiang.config.DataManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.server.db.dao.XyClubInfoDao;
import com.game.server.db.dao.XyClubMemberInfoDao;
import com.game.server.db.dao.XyNotificationLogDao;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.db.entity.XyClubMemberInfo;
import com.game.server.db.entity.XyNotificationLog;
import com.game.server.message.proto.in.AbstractXYbecomeAPartnerReq;
import com.game.server.message.proto.out.XYInformDataAlterRsp;
import com.game.server.message.proto.out.XYbecomeAPartnerRsp;
import com.game.server.message.proto.out.XYmClubSetplayerRsp;
import com.game.server.util.NoticeUtil;
import net.sf.json.JSONObject;

/**
 * 成为合伙人
 */
public class XYbecomeAPartnerReq extends AbstractXYbecomeAPartnerReq {

    @Override
    public void Execute(ISession session) {
        LogicClient client = (LogicClient) LogicServer.getInstance().getClient(session.getId());
        if (client != null&&client.checkClick(msgcode)) {
            XYbecomeAPartnerRsp rsp = new XYbecomeAPartnerRsp();
            rsp.setIsok(1);
            rsp.setType(this.type);
            XyClubInfo clubInfo = DataManager.getInstance().getMapClub().get(this.cid);
            if (clubInfo!=null){
                XyClubMemberInfo clubMemberInfo = clubInfo.getPlayer().get(client.getUserId());
                if (this.type==1){
                    if (clubMemberInfo!=null && clubMemberInfo.getIsAccessory()!=1){
                        int applyCount = XyNotificationLogDao.findByUidApplyCount(client.getUserId(), clubInfo.getId(),1,2);
                        rsp.setIsok(2);
                        if (applyCount==0){
                            rsp.setIsok(0);
                            XyNotificationLog entity = new XyNotificationLog();
                            entity.setCid(clubInfo.getId());
                            entity.setUid(client.getUserId());
                            entity.setNotification_type(1);
                            entity.setMsg_type(2);
                            entity.setState(0);
                            entity.setAdditional_data("");
                            XyNotificationLogDao.insertInto(entity);


                            NoticeUtil.ClubNoticeNotice(client, clubInfo);
                        }
                        /*int updateCount = XyClubMemberInfoDao.updeteIsAccessory(clubInfo.getId(), client.getUserId(), 1);
                        if (updateCount>0){
                            clubMemberInfo.setIsAccessory(1);
                            int xyclubid = 0;
                            while (true){//获取一个未使用的ID
                                xyclubid = DataManager.getInstance().findOnlyID("xy_club_info","id");
                                int count = XyClubMemberInfoDao.findPartnerIdCount(xyclubid);
                                if (count!=0){
                                    break;
                                }
                            }
                            clubMemberInfo.setPartner_id(xyclubid);
                            XyClubMemberInfoDao.updetePartnerId(clubInfo.getId(), client.getUserId(), xyclubid);

                        }*/
                    }
                }
                if (this.type==2){//关闭推广系统
                    if (clubMemberInfo!=null && clubMemberInfo.getIsManager()==1){
                        int updateCount = XyClubInfoDao.updatePromotionType(1,clubInfo.getId());
                        if (updateCount>0){
                            rsp.setIsok(0);
                            clubInfo.setPromotion_type(1);
                        }
                    }
                }

                if (this.type==3){//开启推广系统
                    if (clubMemberInfo!=null && clubMemberInfo.getIsManager()==1){
                        int updateCount = XyClubInfoDao.updatePromotionType(0,clubInfo.getId());
                        if (updateCount>0){
                            rsp.setIsok(0);
                            clubInfo.setPromotion_type(0);
                        }
                    }
                }
            }
            client.Send(rsp);
        }
    }

}
